package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.*;
import com.eastfair.venueservice.entity.ServiceInventoryInFlow;
import com.eastfair.venueservice.service.ServiceInventoryInFlowService;
import com.eastfair.venueservice.vo.ServiceInventoryInFlowVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 服务库存入库流水记录
 * </p>
 *
 * @author linan
 * @date 2022-09-19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/serviceInventoryInFlow")
@Api(value = "ServiceInventoryInFlow", tags = "服务库存入库流水记录")
@PreAuth(replace = "venueservice:serviceInventoryInFlow:")
public class ServiceInventoryInFlowController extends SuperController<ServiceInventoryInFlowService, Long, ServiceInventoryInFlow, ServiceInventoryInFlowPageQuery, ServiceInventoryInFlowSaveDTO, ServiceInventoryInFlowUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<ServiceInventoryInFlow> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ServiceInventoryInFlow> serviceInventoryInFlowList = list.stream().map((map) -> {
            ServiceInventoryInFlow serviceInventoryInFlow = ServiceInventoryInFlow.builder().build();
            //TODO 请在这里完成转换
            return serviceInventoryInFlow;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(serviceInventoryInFlowList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ServiceInventoryInFlowPageQuery> params) {
        ServiceInventoryInFlowPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 插入入库流水
     * @param serviceInventoryInFlowDTO
     * @return R<Boolean>
     */
    @ApiOperation(value = "插入入库流水")
    @SysLog("入库")
    @PostMapping(value = "/saveInFlow")
    public R<Boolean> saveInFlow(@RequestBody ServiceInventoryInFlowDTO serviceInventoryInFlowDTO) {
        try {
            return R.success(baseService.saveInFlow(serviceInventoryInFlowDTO));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 查询入库记录
     * @param serviceInventoryQuery
     * @return
     */
    @ApiOperation(value = "查询入库记录")
    @PostMapping(value = "/queryInFlowList")
    public R<Page<ServiceInventoryInFlowVO>> queryInFlowList(@RequestBody PageParams<ServiceInventoryInFlowQuery> serviceInventoryQuery){
        try {
            return R.success(baseService.queryInFlowList(serviceInventoryQuery));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }
}
